package com.project.frame.common.intercetor;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * desc
 * @author lw007
 * @date 2021/12/23
 */
@Component("authInterceptor")
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
    @Value("${shiro.loginUrl}")
    private String loginUrl;
    @Value("${shiro.successUrl}")
    private String successUrl;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String uri = request.getRequestURI();
        log.info("uri={}",uri);
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            response.sendRedirect(request.getContextPath() + successUrl);
            return false;
        }
        response.sendRedirect(request.getContextPath() + loginUrl);
        return false;
    }

}
